package com.proton.carepatchtemp.utils;

import com.apkfuns.logutils.LogUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.List;

//json解析
public class JSONUtils {
    public static String getString(String jsonStr, String key) {
        JSONObject jObj = getJOSNObj(jsonStr);
        if (jObj == null) {
            return "";
        }
        try {
            return jObj.getString(key);
        } catch (JSONException e) {
            LogUtils.e(e);
        }
        return "";
    }

    public static JSONObject getJOSNObj(String jsonStr) {
        try {
            return new JSONObject(jsonStr);
        } catch (JSONException e) {
            LogUtils.e(e);
        }
        return null;
    }

    public static JSONArray getJSONArray(String jsonStr) {
        try {
            return new JSONArray(jsonStr);
        } catch (JSONException e) {
            LogUtils.e(e);
        }
        return null;
    }


    public static <T> T getObj(String jsonStr, Class<T> classType) {
        try {
            Gson gson = GsonUtils.getGson();
            return gson.fromJson(jsonStr, classType);

        } catch (JsonSyntaxException e) {
            LogUtils.e(e);
        }
        return null;
    }

    public static <T> List<T> getObj(String jsonStr, Type type) {
        try {
            Gson gson = GsonUtils.getGson();
            return gson.fromJson(jsonStr, type);

        } catch (JsonSyntaxException e) {
            LogUtils.e(e);
        }
        return null;
    }

    public static <T> List<T> getObj(String jsonStr, Type type, String format) {
        try {
            Gson gson = GsonUtils.getGson(format);
            return gson.fromJson(jsonStr, type);

        } catch (JsonSyntaxException e) {
            LogUtils.e(e);
        }
        return null;
    }

    public static <T> T getObj(String jsonStr, Class<T> classType, String format) {
        try {
            Gson gson = GsonUtils.getGson(format);
            return gson.fromJson(jsonStr, classType);
        } catch (JsonSyntaxException e) {
            LogUtils.e(e);
        }
        return null;
    }


// --Commented out by Inspection START (2016/3/15 11:17):
//    public static <T> String toJson(T entryList, Type type) {
//        try {
//            return GsonUtils.getGson().toJson(entryList,type);
//        }catch (Throwable e){
//            LogMuch.e(e);
//        }
//        return "";
//    }
// --Commented out by Inspection STOP (2016/3/15 11:17)
}
